package cz.muni.fi.pa165.hauntedhouses.web;

import cz.muni.fi.pa165.hauntedhouses.dto.AccountDTO;
import cz.muni.fi.pa165.hauntedhouses.facade.ServiceFacade;
import java.util.ResourceBundle;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;

/**
 *
 * @author Siliarus
 */
@DoesNotRequireLogin
@UrlBinding("/secure/{event}")
public class SecurityActionBean implements ActionBean {

    private ResourceBundle properties;
    
    private ActionBeanContext context;
    @Validate(required = true, on = "submitLogin")
    private String username;
    @Validate(required = true, on = "submitLogin")
    private String password;
    @Validate(required = true, on = "submitRegister")
    private String registerUsername;
    @Validate(required = true, on = "submitRegister")
    private String registerPassword;

    public String getRegisterUsername() {
        return registerUsername;
    }

    public void setRegisterUsername(String username) {
        this.registerUsername = username;
    }

    public String getRegisterPassword() {
        return registerPassword;
    }

    public void setRegisterPassword(String password) {
        this.registerPassword = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    @SpringBean
    protected ServiceFacade sf;

    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @DefaultHandler
    public Resolution login() {
        return new ForwardResolution("index.jsp");
    }

    public Resolution submitLogin() {
        properties = ResourceBundle.getBundle("StripesResources");
        HttpSession session = context.getRequest().getSession();
        String path = (String) session.getAttribute("userPath");
        AccountDTO acc = sf.loginUser(username, password);
            if (acc != null) {
                session.setAttribute("loggedIn", true);
                if (acc.getIsAdmin()) {
                    return new RedirectResolution(AdminGhostsActionBean.class, "home");
                } else {
                    return new RedirectResolution(PlayActionBean.class, "home");
                }
            } else {
                this.getContext().getValidationErrors().addGlobalError(new SimpleError(properties.getString("error.loginfailed")));
                return new ForwardResolution("index.jsp");
            }
        
    }

    public Resolution submitRegister() {
        properties = ResourceBundle.getBundle("StripesResources");
        HttpSession session = context.getRequest().getSession();
        AccountDTO acc = new AccountDTO();
        if (registerUsername.equals("admin")) {
            acc.setIsAdmin(true);
        } else {
            acc.setIsAdmin(false);
        }
        acc.setPassword(registerPassword);
        acc.setUsername(registerUsername);

        AccountDTO accFromDb = sf.registerUser(acc);
        if (accFromDb != null) {
            session.setAttribute("loggedIn", true);
            if (acc.getIsAdmin()) {
                return new RedirectResolution(AdminGhostsActionBean.class, "home");
            } else {
                return new RedirectResolution(PlayActionBean.class, "home");
            }
        } else {
            this.getContext().getValidationErrors().addGlobalError(new SimpleError(properties.getString("error.registrationfailed")));
            return new ForwardResolution("index.jsp");
        }
    }

    public Resolution logout() {
        context.getRequest().getSession().setAttribute("loggedIn", false);
        return new ForwardResolution("index.jsp");
    }
}
